<?php

class Exts_Views_Helpers_HeadMetaExts extends Zend_View_Helper_HeadMeta {


	protected $_typeKeys = array('name', 'http-equiv', 'charset', 'property');

	public function headMetaExts($content = null, $keyValue = null, $keyType = 'name', $modifiers = array(), $placement = Zend_View_Helper_Placeholder_Container_Abstract::APPEND){
		return parent::headMeta($content, $keyValue, $keyType, $modifiers, $placement);
	}

	public function __call($method, $args) {
		if (preg_match('/^(?P<action>set|(pre|ap)pend|offsetSet)(?P<type>Name|HttpEquiv|Property)$/', $method, $matches)) {
			$action = $matches['action'];
			$type = $this->_normalizeType($matches['type']);
			$argc = count($args);
			$index = null;

			if ('offsetSet' == $action) {
				if (0 < $argc) {
					$index = array_shift($args);
					--$argc;
				}
			}

			if (2 > $argc) {
				require_once 'Zend/View/Exception.php';
				$e = new Zend_View_Exception('Too few arguments provided; requires key value, and content');
				$e->setView($this->view);
				throw $e;
			}

			if (3 > $argc) {
				$args[] = array();
			}
			
			$item = $this->createData($type, $args[0], $args[1], array());

			if ('offsetSet' == $action) {
				return $this->offsetSet($index, $item);
			}

			$this->$action($item);
			return $this;
		}

		return parent::__call($method, $args);
	}

	protected function _normalizeType($type) {
		switch ($type) {
			case 'Property':
				return 'property';
			default:
				return parent::_normalizeType($type);
		}
	}

}
